package net.vkits.common.listeners;

import net.vkits.anjiamiao.domain.event.*;
import net.vkits.common.sms.SmsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;

import javax.annotation.Resource;

/**
 * Created by ethan-liu on 2017/1/7.
 */
@Component
public class SmsListener {
    private static final Logger logger = LoggerFactory.getLogger(SmsListener.class);

    @Resource
    private SmsService smsService;

    @TransactionalEventListener
    public void handleVerifyCodeEvent(VerifyCodeEvent verifyCodeEvent) {
        smsService.sendVerifyCode(verifyCodeEvent.getCellPhoneNum(), verifyCodeEvent.getVerifyCode());
    }

    @TransactionalEventListener
    public void handlePayEvent(PayEvent payEvent) {
        logger.info("payEvent: {}", payEvent);
    }

    @TransactionalEventListener
    public void handleTransferSuccessEvent(TransferSuccessEvent event) {
        smsService.sendTransferSuccess(event);
        if (event.getNotifyLandlord()) {
            smsService.sendLandlordTransferSuccess(event);
        }
    }

    @TransactionalEventListener
    public void handleTransferFailureEvent(TransferFailureEvent event) {
        smsService.sendTransferFailure(event.getCellPhoneNum());
    }

    @TransactionalEventListener
    public void handleRiskWarningEvent(RiskWarningEvent event) {
        smsService.sendRiskWarning(event.getCellPhoneNum());
    }

    @TransactionalEventListener
    public void handleRefundEvent(RefundEvent event) {
        smsService.sendRefundnotice(event.getCellPhoneNum(), event.getRentAmount());
    }
}
